#include "include/thesauruscomposerwidget.h"
#include "ui_thesauruscomposerwidget.h"

#include <QDebug>

#include "include/Common.h"
#include "include/dictionarylookup.h"

ThesaurusComposerWidget::ThesaurusComposerWidget(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ThesaurusComposerWidget)
{
	ui->setupUi(this);
}

ThesaurusComposerWidget::~ThesaurusComposerWidget()
{
	delete ui;
}

void ThesaurusComposerWidget::on_textEdit_selectionChanged()
{
	QString text = ui->textEdit->textCursor().selectedText();
	if (text != "")
	{
		DictionaryLookup* p = new DictionaryLookup(text);
		m_lock.lock();
		m_lookups.insert(p);
		m_lock.unlock();
		connect(p, SIGNAL(gotDefinition(DictionaryLookup*)), this, SLOT(gotDefinition(DictionaryLookup*)));
		connect(p, SIGNAL(gotSynonyms(DictionaryLookup*)), this, SLOT(gotSynonyms(DictionaryLookup*)));
		p->performLookup();
		p->performSynonymsLookup();
	}
}

void ThesaurusComposerWidget::gotDefinition(DictionaryLookup* p)
{
	ui->plainTextEdit->clear();
	ui->plainTextEdit->insertPlainText(p->getRawLookup());
}

void ThesaurusComposerWidget::gotSynonyms(DictionaryLookup* p)
{
	ui->plainTextEdit_2->clear();
	ui->plainTextEdit_2->insertPlainText(p->getRawSynonyms());
}
